// pages/driver/driver.js
const db = wx.cloud.database()
const driver_account = db.collection('driver_account')
Page({
  /**
   * 页面的初始数据
   */
  data: {
    driverNumber: '',
    driver_id: '',
    modal: false,
    modalTitle: '输入新的账号/密码',
    changeId: '',
    changePassword: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      driverNumber: options.driverNumber,
      driver_id: options.driver_id
    })
  },
  onExit() {
    let that = this
    wx.showLoading({
      title: '加载中',
    })
    that.setWorkState(false)
    //修改driver_account表
    wx.cloud.callFunction({
      name: 'setDriverAccount',
      data: {
        number: that.data.driverNumber,
        state: false,
        work_state:false
      }
    }).then(res => {
      // 关闭加载
      wx.hideLoading()
      wx.reLaunch({
        url: '/pages/index/index'
      })
    })
  },
  onSet() {
    this.setData({
      modal: !this.data.modal
    })
  },
  onSetWX() {
    wx.openSetting()
  },
  onConfirm() {
    let that = this
    //判断账号和密码是否符合规格
    if (that.data.changeId == '' || that.data.changePassword == '') {
      console.log('密码和账号不能为空')
    } else if (!(/(^[0-9]*$)/.test(this.data.changeId))) {
      console.log('账号只能是数字')
    } else if (!(/^[A-Za-z0-9]+$/.test(that.data.changePassword))) {
      console.log('账号只能是数字或字母')
    } else {
      //判断账号是否重复
      driver_account.get().then(res => {
        console.log('成功', res.data, that.data.changeId, that.data.driverNumber)
        let f = true
        res.data.some((item) => {
          if (item.driver_id === that.data.changeId && item.number !== that.data.driverNumber) {
            console.log('账号重复')
            f = false
            return
          }
        })
        if (f) {
          console.log('云')
          wx.showLoading({
            title: '加载中',
          })
          wx.cloud.callFunction({
            name: 'updateDriverAccount',
            data: {
              number: that.data.driverNumber,
              driver_id:that.data.changeId,
              driver_password:that.data.changePassword
            }
          }).then(() => {
            // 关闭加载
            wx.hideLoading()
            wx.showToast({
              title: '修改成功',
              icon: 'success',
              duration: 2000,
              success(){
                that.onExit()
              }
            })
            that.setData({
              modal:false
            })
          })
        }
      }).catch(err => {
        console.log('失败', err)
      })
    }
  },
  changeId(e) {
    console.log(e.detail.value)
    this.setData({
      changeId: e.detail.value
    })
  },
  changePassword(e) {
    console.log(e.detail.value)
    this.setData({
      changePassword: e.detail.value
    })
  },

  //调用云函数setWorkState云函数，更新司机工作状态
  setWorkState(workState) {
    let that = this
    wx.cloud.callFunction({
      name: 'setWorkState',
      data: {
        work_state: workState,
        number: that.data.driverNumber
      }
    })
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})